﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.UI.WebControls;
using Controllerss;
using Core.Contract.DataContract;
using www.Controllers;
//using www.SService;
using www.Controllerss;
using www.Core.Contract.DataContract;

namespace www.Admin
{
    public partial class Menu : System.Web.UI.Page
    {
        #region Value

        //private readonly HyServicesClient _service1 = new HyServicesClient();
        private readonly LanguageController _languageController=new LanguageController();
        private readonly MenuController _menuController=new MenuController();
        private readonly CategoryController _categoryController=new CategoryController();
        private readonly ObjectCache _cache = MemoryCache.Default;
        private static Boolean _insertNew = false;
        private static Boolean insertNew = false;
        private static List<RelaxMenuDataContract> _lmMenuDataContracts;
        private static long _id;
        #endregion

        #region Contructor

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToInt32(Session["role"]) != 1 && Convert.ToInt32(Session["role"]) != 2)
            {
                Message("Bạn không đủ quyền để thực hiện chức năng này.");
                Response.Redirect("Default.aspx");
            }

            if (Cache["Language"] == null)
            {
                var lLanguage = _languageController.GetLanguageDataContracts();
                if (lLanguage.Any())
                    Cache.Insert("Language", lLanguage.ToList(), null, DateTime.MaxValue, TimeSpan.FromDays(30));
            }

            if (!IsPostBack)
            {
                var lLanguages = (List<RelaxLanguageDataContract>)Cache["Language"];
                if (lLanguages != null)
                {
                    var lReturn = lLanguages.Where(l => l.Id != 1 && l.Type == 1);
                    Addlist(lReturn);
                }
                LoadCategory();
                LoadData();
                var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                AddListMenu(lData);
            }
        }
        #endregion

        #region Event

        protected void lbtInsert_Click(object sender, EventArgs e)
        {
            try
            {
                pnwrapView.Visible = false;
                pnview.Visible = false;
                pnposition.Visible = false;
                pnControl.Visible = true;
                _insertNew = true;
                insertNew = false;
                Donew();
                _lmMenuDataContracts = null;
                grvNgonngu.DataSource = null;
                grvNgonngu.DataBind();
            }
            catch (Exception)
            {

            }
        }
        protected void lbtback_Click(object sender, EventArgs e)
        {
            try
            {
                Back();
            }
            catch (Exception)
            {

            }
        }

        protected void lbtrefresh_Click(object sender, EventArgs e)
        {
            try
            {
                LoadData();
            }
            catch (Exception)
            {

            }

        }
        protected void lbtOrder_Click(object sender, EventArgs e)
        {
            try
            {
                Order();
            }
            catch (Exception)
            {

            }

        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            try
            {
                Save();
            }
            catch (Exception)
            {

            }

        }
        protected void lbtSaveMenu_Click(object sender, EventArgs e)
        {
            try
            {
                SaveMenu();
            }
            catch (Exception)
            {

            }

        }
        protected void grid_advertise_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grid_menu.PageIndex = e.NewPageIndex;
                LoadData();
            }
            catch (Exception)
            {

            }
        }

        protected void grid_advertise_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "cmd_active":
                        var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                        if (lData == null)
                            return;
                        var first = lData.FirstOrDefault(p => p.Id == Convert.ToInt32(e.CommandArgument) && p.Languagecode == 1);
                        if (first == null)
                            return;

                        first.Active = first.Active == 1 ? 0 : 1;
                        if (_menuController.Update(new List<RelaxMenuDataContract> { first }))
                        {
                            _cache["Menu"] = lData;
                            Message("Cập nhật Thành Công");
                        }
                        break;
                    case "cmd_edit":
                        _id = Convert.ToInt32(e.CommandArgument);
                        var lData1 = (List<RelaxMenuDataContract>)_cache["Menu"];
                        if (lData1 == null)
                            return;
                        var firsts = lData1.FirstOrDefault(p => p.Id == _id && p.Languagecode == 1);
                        if (firsts == null)
                            return;
                        txtDecription.Value = firsts.Decription;
                        txtKeyword.Value = firsts.Keyword;
                        txtLink.Value = firsts.Link;
                        txtName.Value = firsts.Name;
                        txtOrd.Value = firsts.Ord.ToString();
                        ddlCategory.SelectedValue = firsts.CategoryId.ToString();
                        ddlType.SelectedValue = firsts.Type.ToString();
                        ddlParent.SelectedValue = firsts.Parent;
                        ddlParent.Enabled = firsts.Type != 2;
                        ddlTarget.SelectedValue = firsts.Target;
                        chkActive.Checked = firsts.Active == 1;
                        var lValue = lData1.Where(p => p.Id == _id && p.Languagecode != 1).ToList();
                        _lmMenuDataContracts = lValue;
                        grvNgonngu.DataSource = _lmMenuDataContracts;
                        grvNgonngu.DataBind();
                        pnControl.Visible = true;
                        pnposition.Visible = false;
                        pnview.Visible = false;
                        pnwrapView.Visible = false;
                        break;
                }

            }
            catch (Exception)
            {

            }
        }
        protected void ddlTypeMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadData();
                var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                AddList(lData);
            }
            catch (Exception)
            {

            }
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                if (Convert.ToInt32(ddlType.SelectedValue) == 2)
                {
                    ddlParent.Enabled = false;
                    AddListMenu(lData);
                }
                else
                {
                    ddlParent.Enabled = true;
                    AddListMenu(lData);
                }
            }
            catch (Exception)
            {

            }
        }
        protected void lbtDonew_Click(object sender, EventArgs e)
        {
            try
            {
                Donew();
            }
            catch (Exception)
            {

            }
        }

        protected void btnThemValue_Click(object sender, EventArgs e)
        {
            try
            {
                var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                InsertLanguageCode(lData, _id);
                txtNameValue.Value = txtDecriptionValue.Value = txtKeywordValue.Value = string.Empty;
                insertNew = false;
            }
            catch (Exception)
            {

            }
        }

        protected void grvNgonngu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {

                var first = _lmMenuDataContracts.FirstOrDefault(p => p.Languagecode == Convert.ToInt32(e.CommandArgument));
                if (first == null)
                    return;
                if (e.CommandName == "cmd_edit")
                {
                    EditLanguage(Convert.ToInt32(e.CommandArgument));
                    insertNew = true;
                }
                else if (e.CommandName == "cmd_delete")
                {
                    DeleteLanguage(Convert.ToInt32(e.CommandArgument));
                }
            }
            catch (Exception)
            {

            }
        }

        protected void grid_menu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var mnuId = ((Label)e.Row.FindControl("lblParent")).Text;
                    var catId = ((Label)e.Row.FindControl("lblCategoryId")).Text;
                    if (!string.IsNullOrEmpty(mnuId) || Convert.ToInt32(mnuId) > 0)
                    {
                        var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                        var first = lData.FirstOrDefault(p => p.Id == Convert.ToInt32(mnuId));
                        if (first != null)
                        {
                            var parentName = (Label)e.Row.FindControl("lblParentDisplay");
                            parentName.Text = first.Name;
                        }
                    }
                    if (!string.IsNullOrEmpty(catId) || Convert.ToInt32(catId) > 0)
                    {
                        var lData = (List<RelaxCategoryDataContract>)_cache["Category"];
                        if (lData != null)
                        {
                            var first = lData.FirstOrDefault(p => p.Id == Convert.ToInt32(catId));
                            if (first != null)
                            {
                                var catName = (Label)e.Row.FindControl("lblCategoryName");
                                catName.Text = first.Name;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region Business

        private void LoadCategory()
        {
            if (_cache["Category"] == null)
            {
                var lData = _categoryController.GetCategoryDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Category", lData.ToList(), policy);
                }
            }
            ddlCategory.Items.Clear();
            ddlCategory.Items.Add(new ListItem("---Chọn---", "0"));
            var lData1 = (List<RelaxCategoryDataContract>)_cache["Category"];
            if (lData1 != null)
            {
                var lReturn =
                    lData1.Where(
                        p =>
                            p.Languagecode == 1 && p.Active == Constant.Active.Display).ToList();

                foreach (var item in lReturn.Where(p => p.Type <= 0))
                {
                    ddlCategory.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                    var lReturn1 = lReturn.Where(p => p.Type == item.Id).ToList();
                    if (lReturn1.Any())
                    {
                        foreach (var item1 in lReturn1)
                        {
                            ddlCategory.Items.Add(new ListItem("--" + item1.Name, item1.Id.ToString()));
                        }
                    }
                }
            }
        }

        private void Save()
        {
            if (string.IsNullOrEmpty(ddlmenu.Value))
            {
                Message("bạn phải chọn menu cần sắp xếp.");
                return;
            }
            if (string.IsNullOrEmpty(txtord2.Value))
            {
                Message("Nhập vào thứ tự của menu bạn muốn sắp xếp lại.");
                return;
            }
            int ord;
            if (!int.TryParse(txtord2.Value, out ord))
            {
                Message("bạn phải nhập vào là số nguyên.");
                return;
            }
            var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
            var first =
                lData.FirstOrDefault(p => p.Id == Convert.ToInt32(string.IsNullOrEmpty(ddlmenu.Value) ? "0" : ddlmenu.Value));
            if (first != null)
            {
                first.Ord = ord;
                if (_menuController.Update(new List<RelaxMenuDataContract> { first }))
                {
                    _cache["Menu"] = lData;
                    Message("Sắp xếp thành công.");
                }
            }
        }

        private void SaveMenu()
        {
            if (string.IsNullOrEmpty(txtName.Value) || string.IsNullOrEmpty(txtLink.Value) ||
                string.IsNullOrEmpty(txtKeyword.Value) || string.IsNullOrEmpty(txtDecription.Value) || string.IsNullOrEmpty(txtOrd.Value))
            {
                Message("bạn phải nhập đầy đủ các gợi ý trên.");
                return;
            }
            if (Convert.ToInt32(ddlType.SelectedValue) == 1)
            {
                if (Convert.ToInt32(ddlCategory.SelectedValue) <= 0)
                {
                    Message("bạn phải chưa chọn danh mục.");
                    return;
                }
            }
            int ord;
            if (!int.TryParse(txtOrd.Value, out ord))
            {
                Message("Thứ tự nhập vào phải là số.");
                return;
            }
            if (ddlParent.Enabled && Convert.ToInt32(ddlParent.SelectedValue) <= 0)
            {
                Message("bạn phải chọn menu cha.");
                return;
            }
            var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
            if (_insertNew)
            {
                var lMenuItem = new List<RelaxMenuDataContract>();
                var menuItem = new RelaxMenuDataContract
                {
                    Name = txtName.Value,
                    Tag = txtName.Value.Replace(' ', '-'),
                    Keyword = txtKeyword.Value,
                    Decription = txtDecription.Value,
                    Ord = ord,
                    Active = chkActive.Checked ? 1 : 0,
                    Target = ddlTarget.SelectedValue,
                    Link = txtLink.Value,
                    CategoryId = Convert.ToInt32(ddlCategory.SelectedValue),
                    Languagecode = 1,
                    Type = Convert.ToInt32(ddlType.SelectedValue)
                };
                if (!string.IsNullOrEmpty(ddlParent.SelectedValue) && Convert.ToInt32(ddlParent.SelectedValue) > 0 && ddlParent.Enabled)
                    menuItem.Parent = ddlParent.SelectedValue;

                lMenuItem.Add(menuItem);
                if (_lmMenuDataContracts != null)
                {
                    lMenuItem.AddRange(_lmMenuDataContracts);
                }
                var id = _menuController.Add(lMenuItem);

                foreach (var item in lMenuItem)
                {
                    item.Id = id;
                    item.Link = lMenuItem[0].Link;
                    item.Ord = lMenuItem[0].Ord;
                    item.Parent = lMenuItem[0].Parent;
                    item.Target = lMenuItem[0].Target;
                    item.CategoryId = lMenuItem[0].CategoryId;
                    item.Active = lMenuItem[0].Active;
                    item.Type = lMenuItem[0].Type;
                }
                if (lData == null)
                    lData = new List<RelaxMenuDataContract>();
                lData.AddRange(lMenuItem);
                _cache["Menu"] = lData;
                Message("Thêm thành công !");
                _lmMenuDataContracts = null;
                grvNgonngu.DataSource = null;
                grvNgonngu.DataBind();
            }
            else
            {

                var lMenuItem = new List<RelaxMenuDataContract>();
                var menuItem = new RelaxMenuDataContract
                {
                    Id = _id,
                    Name = txtName.Value,
                    Tag = txtName.Value.Replace(' ', '-'),
                    Keyword = txtKeyword.Value,
                    Decription = txtDecription.Value,
                    Ord = ord,
                    Active = chkActive.Checked ? 1 : 0,
                    Target = ddlTarget.SelectedValue,
                    Link = txtLink.Value,
                    CategoryId = Convert.ToInt32(ddlCategory.SelectedValue),
                    Languagecode = 1,
                    Type = Convert.ToInt32(ddlType.SelectedValue)
                };
                if (!string.IsNullOrEmpty(ddlParent.SelectedValue) && Convert.ToInt32(ddlParent.SelectedValue) > 0 && ddlParent.Enabled)
                    menuItem.Parent = ddlParent.SelectedValue;
                lMenuItem.Add(menuItem);
                if (_lmMenuDataContracts != null)
                {
                    lMenuItem.AddRange(_lmMenuDataContracts);
                }
                if (_menuController.Update(lMenuItem))
                {
                    foreach (var item in lMenuItem)
                    {

                        var first = lData.FirstOrDefault(p => p.Id == item.Id && p.Languagecode == item.Languagecode);
                        if (first != null)
                        {
                            first.Keyword = item.Keyword;
                            first.Decription = item.Decription;
                            first.Link = lMenuItem[0].Link;
                            first.Name = item.Name;
                            first.Ord = lMenuItem[0].Ord;
                            first.Parent = lMenuItem[0].Parent;
                            first.Tag = item.Tag;
                            first.Target = lMenuItem[0].Target;
                            first.CategoryId = lMenuItem[0].CategoryId;
                            first.Active = lMenuItem[0].Active;
                            first.Type = lMenuItem[0].Type;
                        }
                        else
                        {
                            item.Id = lMenuItem[0].Id;
                            item.Link = lMenuItem[0].Link;
                            item.Ord = lMenuItem[0].Ord;
                            item.Parent = lMenuItem[0].Parent;
                            item.Target = lMenuItem[0].Target;
                            item.CategoryId = lMenuItem[0].CategoryId;
                            item.Active = lMenuItem[0].Active;
                            item.Type = lMenuItem[0].Type;
                            lData.Add(item);
                        }
                    }
                    Message("Sửa thành công !");
                    _cache["Menu"] = lData;

                    LoadData();
                    _lmMenuDataContracts = null;
                    grvNgonngu.DataSource = null;
                    grvNgonngu.DataBind();
                    pnControl.Visible = false;
                    pnposition.Visible = false;
                    pnwrapView.Visible = true;
                    pnview.Visible = true;
                }
            }
        }
        private void InsertLanguageCode(IEnumerable<RelaxMenuDataContract> lData, long iD = 0)
        {

            if (!string.IsNullOrEmpty(ddlLanguage.SelectedValue))
            {
                if (iD > 0)
                {
                    if (_lmMenuDataContracts == null)
                    {
                        if (lData != null)
                        {
                            var lVaue =
                                lData.Where(p => p.Id == iD && p.Languagecode != 1).ToList();
                            if (!lVaue.Any())
                                lVaue = new List<RelaxMenuDataContract>();
                            _lmMenuDataContracts = lVaue;
                        }
                        else
                            _lmMenuDataContracts = new List<RelaxMenuDataContract>();
                    }

                    var languageCode = Convert.ToInt32(ddlLanguage.SelectedValue);
                    var check = _lmMenuDataContracts.Where(p => p.Languagecode == languageCode).ToList();
                    if (check.Any())
                    {
                        if (insertNew)
                        {
                            var firstordefault = check.FirstOrDefault();
                            if (firstordefault != null)
                            {
                                firstordefault.Languagecode = languageCode;
                                firstordefault.Name = txtNameValue.Value;
                                firstordefault.Tag = txtNameValue.Value.Replace(' ', '-');
                                firstordefault.Decription = txtDecriptionValue.Value;
                                firstordefault.Keyword = txtKeywordValue.Value;
                            }
                        }
                        else
                        {
                            Message("Ngôn ngữ này đã tồn tại.");
                            return;
                        }
                    }
                    else
                    {
                        var advertise = new RelaxMenuDataContract
                        {
                            Id = _id,
                            Languagecode = languageCode,
                            Name = txtNameValue.Value,
                            Tag = txtNameValue.Value.Replace(' ', '-'),
                            Decription = txtDecriptionValue.Value,
                            Keyword = txtKeywordValue.Value
                        };
                        _lmMenuDataContracts.Add(advertise);
                    }

                }
                else
                {
                    if (_lmMenuDataContracts == null)
                        _lmMenuDataContracts = new List<RelaxMenuDataContract>();

                    var languageCode = Convert.ToInt32(ddlLanguage.SelectedValue);
                    var check = _lmMenuDataContracts.Where(p => p.Languagecode == languageCode).ToList();
                    if (check.Any())
                    {
                        if (insertNew)
                        {
                            var firstordefault = check.FirstOrDefault();
                            if (firstordefault != null)
                            {
                                firstordefault.Languagecode = languageCode;
                                firstordefault.Name = txtNameValue.Value;
                                firstordefault.Tag = txtNameValue.Value.Replace(' ', '-');
                                firstordefault.Decription = txtDecriptionValue.Value;
                                firstordefault.Keyword = txtKeywordValue.Value;
                            }
                        }
                        else
                        {
                            Message("Ngôn ngữ này đã tồn tại.");
                            return;
                        }
                    }
                    else
                    {
                        var advertise = new RelaxMenuDataContract
                        {
                            Languagecode = languageCode,
                            Name = txtNameValue.Value,
                            Tag = txtNameValue.Value.Replace(' ', '-'),
                            Decription = txtDecriptionValue.Value,
                            Keyword = txtKeywordValue.Value
                        };
                        _lmMenuDataContracts.Add(advertise);
                    }

                }
                grvNgonngu.DataSource = _lmMenuDataContracts;
                grvNgonngu.DataBind();

            }
            else
            {
                Message("Bạn phải chọn ngôn ngữ.");
            }
        }

        private void EditLanguage(int iLanguageCode)
        {
            if (iLanguageCode <= 0) return;
            var first = _lmMenuDataContracts.FirstOrDefault(p => p.Languagecode == iLanguageCode);
            if (first != null)
            {
                txtNameValue.Value = first.Name;
                txtDecriptionValue.Value = first.Decription;
                txtKeywordValue.Value = first.Keyword;
                ddlLanguage.SelectedValue = first.Languagecode.ToString();
            }
        }
        private void DeleteLanguage(int iLanguageCode)
        {

            if (iLanguageCode <= 0) return;

            if (_id > 0)
            {
                _categoryController.Delete(new List<long> { _id }, iLanguageCode);
                if (_lmMenuDataContracts != null)
                {
                    var first = _lmMenuDataContracts.FirstOrDefault(p => p.Languagecode == iLanguageCode);
                    _lmMenuDataContracts.Remove(first);
                }
            }
            else
            {
                if (_lmMenuDataContracts != null)
                {
                    var first = _lmMenuDataContracts.FirstOrDefault(p => p.Languagecode == iLanguageCode);
                    _lmMenuDataContracts.Remove(first);
                }
            }
            grvNgonngu.DataSource = _lmMenuDataContracts;
            grvNgonngu.DataBind();
        }
        private void Addlist(IEnumerable<RelaxLanguageDataContract> lLanguageDataContracts)
        {
            ddlLanguage.Items.Clear();
            ddlLanguage.Items.Add(new ListItem("---Chọn---", "0"));
            foreach (var item in lLanguageDataContracts)
            {
                var items = new ListItem(item.Name, item.Id.ToString());
                ddlLanguage.Items.Add(items);
            }

        }
        private void AddList(IEnumerable<RelaxMenuDataContract> lMenuDataContracts)
        {
            ddlmenu.Items.Clear();
            ddlmenu.Items.Add(new ListItem("---Chọn---", "0"));
            if (lMenuDataContracts != null)
            {
                var lReturn = lMenuDataContracts.Where(p => p.Languagecode == 1);
                foreach (var item in lReturn)
                {
                    ddlmenu.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                }
            }
        }
        private void AddListMenu(IEnumerable<RelaxMenuDataContract> lMenuDataContracts)
        {
            ddlParent.Items.Clear();
            ddlParent.Items.Add(new ListItem("---Chọn---", "0"));
            if (lMenuDataContracts != null)
            {
                var lReturn = lMenuDataContracts.Where(p => p.Languagecode == 1 && p.Type == Convert.ToInt32(ddlType.SelectedValue)).ToList();
                foreach (var item in lReturn.Where(p => p.Parent == null))
                {
                    ddlParent.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                    AddMenu(lReturn, item.Id, 1);
                }
            }
        }

        private void AddMenu(IEnumerable<RelaxMenuDataContract> lData, long Id, int i)
        {
            if (lData == null)
                return;
            var lReturn = lData.Where(p => p.Parent == Convert.ToString(Id));
            foreach (var item in lReturn)
            {
                ddlParent.Items.Add(new ListItem(item.Name.PadLeft(item.Name.Length + i, '-'), item.Id.ToString()));
                var lReturn1 = lData.Where(p => p.Parent == Convert.ToString(item.Id));
                if (lReturn1.Any())
                {
                    i += 1;
                    AddMenu1(lData, item.Id, i);
                }
            }
        }
        private void AddMenu1(IEnumerable<RelaxMenuDataContract> lData, long id, int i)
        {
            if (lData == null)
                return;
            var lReturn = lData.Where(p => p.Parent == Convert.ToString(id));
            foreach (var item in lReturn)
            {
                ddlParent.Items.Add(new ListItem(item.Name.PadLeft(item.Name.Length + i, '-'), item.Id.ToString()));
                var lReturn1 = lData.Where(p => p.Parent == Convert.ToString(item.Id));
                if (lReturn1.Any())
                {
                    i += 1;
                    AddMenu1(lData, item.Id, i);
                }
            }
        }
        private void Order()
        {
            pnview.Visible = false;
            pnposition.Visible = true;
        }
        private void Back()
        {
            pnview.Visible = true;
            pnposition.Visible = false;
            pnwrapView.Visible = true;
            pnControl.Visible = false;
            _insertNew = false;
            insertNew = false;
            _lmMenuDataContracts = null;
            _id = 0;
            LoadData();
        }

        private void LoadData()
        {
            grid_menu.DataSourceID = null;

            if (_cache["Menu"] == null)
            {
                var lData = _menuController.GetMenuDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Menu", lData.ToList(), policy);
                    var lReturn =
                        from m in
                            lData.Where(p => p.Languagecode == 1 && p.Type == Convert.ToInt32(ddlTypeMenu.SelectedValue))
                        select new { m.Active, m.Decription, m.Id, m.Keyword, m.Languagecode, m.Link, m.Name, m.Ord, m.Parent, m.Tag, m.Target, m.CategoryId, m.Type };
                    grid_menu.DataSource = lReturn.ToList();
                    grid_menu.DataBind();
                }
            }
            else
            {
                var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
                var lReturn =
                         from m in
                             lData.Where(p => p.Languagecode == 1 && p.Type == Convert.ToInt32(ddlTypeMenu.SelectedValue))
                         select new { m.Active, m.Decription, m.Id, m.Keyword, m.Languagecode, m.Link, m.Name, m.Ord, m.Parent, m.Tag, m.Target, m.CategoryId, m.Type };
                grid_menu.DataSource = lReturn.ToList();
                grid_menu.DataBind();
            }
        }

        private void Message(string str)
        {
            Response.Write("<script>alert('" + str + "');</script>");
        }

        private void Donew()
        {
            txtName.Value =
                txtLink.Value =
                    txtDecription.Value =
                        txtDecriptionValue.Value =
                            txtKeyword.Value = txtKeywordValue.Value = txtNameValue.Value = txtOrd.Value = string.Empty;
            ddlLanguage.SelectedIndex = 0;

            ddlCategory.SelectedIndex = 0;
            ddlParent.Items.Clear();
            grvNgonngu.DataSource = null;
            grvNgonngu.DataBind();

        }
        #endregion
      
    }
}